<?php

require_once(APPPATH . 'controllers/frontend/layout.php');

class Cart extends Layout {

    public $_format = "Y-m-d H:i:s";  
        
    public function index() {
        $this->_data['data']['items'] = $this->cart->contents();
        $this->_data['title'] = "Giỏ hàng";
        $this->_data['temp'] = "frontend/cart/index";
        $this->load->view('frontend/layout/index', $this->_data);
    }

    public function insert() {
        if ($this->input->post('btnAdd')) {
            $post = $this->input->post('data');
            $size = $this->input->post('size');
            $bonus = $this->input->post('bonus');
            $color = $this->input->post('color');
            $pro = $this->mproducts->fetchOne($post['id']);
            $post['options']['img'] = $pro->img;       
            if ($size == 0) {
                $post['options']['size'] = $pro->size;
            } else {
                $oppa['size'] = $this->msize->fetchOne($size);
                $post['options']['size'] = $oppa['size']->size;
            }
            if ($bonus != 0) {
                $oppa['bonus'] = $this->mbonus->fetchOne($bonus);
                $post['options']['bonus'] = $oppa['bonus']->bonus;
            } else
                $post['options']['bonus'] = "Không";
            if ($color != 0) {
                $oppa['color'] = $this->mcolor->fetchOne($color);
                $post['options']['color'] = $oppa['color']->color;
                $post['options']['img'] = $oppa['color']->img;
            } else
                $post['options']['color'] = "Mặc định";

            $post['name'] = $pro->name;
            $this->cart->insert($post);
        }
        redirect("frontend/cart");
    }

    public function delete($rowid) {
        $data = array(
            'rowid' => $rowid,
            'qty' => 0
        );
        $this->cart->update($data);
        redirect("frontend/cart");
    }

    public function deleteAll() {
        $this->cart->destroy();
        redirect("frontend/cart");
    }

    public function update() {
        if ($this->input->post('update')) {
            for ($i = 1; $i <= $this->cart->total_items(); $i++) {
                $data[$i - 1] = $this->input->post($i);
            }
            $this->cart->update($data);
        }
        redirect("frontend/cart");
    }

    public function payment() {
        if ($this->cart->total_items() == 0)
            redirect("frontend/cart");
        if ($this->session->userdata('customer')) {
            $this->_data['data']['cus'] = $this->session->userdata('customer');
            $cus_id = $this->_data['data']['cus']['id'];
        }
        if ($this->input->post("order")) {
            $post = $this->input->post("data");
            $this->form_validation->set_error_delimiters('<label class="error">', '</label>');
            $this->form_validation->set_rules('data[email]', 'Email', 'trim|required|valid_email|xss_clean');
            $this->form_validation->set_rules('data[name]', 'Tên', 'trim|required');
            $this->form_validation->set_rules('data[req]', 'Tên', 'trim');
            $this->form_validation->set_rules('data[phone]', 'Số điện thoại', 'trim|required|integer|min_length[7]|max_length[13]');
            $this->form_validation->set_rules('data[address]', 'Địa chỉ', 'trim|required');
            $this->form_validation->set_rules('data[deli_date]', 'Ngày giao hàng', 'trim|required');
            if ($this->form_validation->run() == TRUE) {
                $post['cus_id'] = isset($cus_id) ? $cus_id : 0;
                $post['order_time'] = gmdate($this->_format, time() + 7 * 3600);
                $post['status'] = 0;
                $post['total'] = $this->cart->total();
                $this->morders->insert($post);
                $data['order_id'] =  $this->morders->_last_id;
                $items = $this->cart->contents();
                foreach($items as $item){
                    $data['pro_id'] = $item['id'];
                    $data['name'] = $item['name'];
                    $data['color'] = $item['options']['color'];
                    $data['bonus'] = $item['options']['bonus'];
                    $data['img'] = $item['options']['img'];
                    $data['size'] = $item['options']['size'];
                    $data['qty'] = $item['qty'];
                    $data['subtotal'] = $item['subtotal'];
                    $this->morders_detail->insert($data);
                    $id = $item['id'];
                    $qty = $item['qty'];
                    $this->mproducts->updateQty1($id, $qty);
                }
                $this->cart->destroy();
                $cookie = array(
                    'name' => 'order',
                    'value' => $this->encrypt->encode($this->morders->_last_id),
                    'expire' => '5',
                );

                $this->input->set_cookie($cookie);
                redirect("frontend/cart/order");    
            }
        }
        
        $this->_data['title'] = "Thanh toán";
        $this->_data['temp'] = "frontend/cart/payment";
        $this->load->view('frontend/layout/index', $this->_data);
    }

    public function order() {
        if(isset($_COOKIE['order']) && !empty($_COOKIE['order'])) {
            $cookie = $this->encrypt->decode($_COOKIE['order']);
            $this->_data['data']['order'] = $this->morders->fetchOne($cookie);
            $this->_data['data']['items'] = $this->morders_detail->getByOrderId($cookie);
            $this->_data['title'] = "Đơn hàng";
            $this->_data['temp'] = "frontend/cart/order";
            $this->load->view('frontend/layout/index', $this->_data);
        } 
        else {
            redirect("frontend/home");
        }
    }

}
